package com.voute.View.Fragement;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.Base.BaseFragment;
import com.voute.Model.Action.SelectedHotWordsAction;
import com.voute.Model.Bean.HotWordEntity;
import com.voute.Model.Bean.HotWordsBean;
import com.voute.Model.Bean.SearchResultBean;
import com.voute.Presenter.ISearchPresenter;
import com.voute.Presenter.Impl.SearchPresenterImpl;
import com.voute.R;
import com.voute.Utils.RxBus;
import com.voute.View.Adapter.HotWordsAdapter;
import com.voute.View.IView.ISearchView;

import java.util.ArrayList;

/**
 * 热词Fragment
 * Created by liucxu on 2017/1/14.
 */

public class HotWordsFragment extends BaseFragment implements ISearchView {

    private ListView content_list;
    private ISearchPresenter presenter;
    private ArrayList<HotWordEntity> entities = new ArrayList<>();
    private MaterialDialog progressDialog;
    private HotWordsAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot_words;
    }

    @Override
    public void setupView() {
        content_list = $(R.id.content_list);
        presenter = new SearchPresenterImpl(this);
        progressDialog = new MaterialDialog.Builder(getActivity())
                .progress(true,0)
                .title("提示")
                .content("请等待...")
                .autoDismiss(false)
                .cancelable(true)
                .build();
        adapter=new HotWordsAdapter(getActivity(),entities);
        content_list.setAdapter(adapter);
        content_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SelectedHotWordsAction action=new SelectedHotWordsAction();
                action.setSelected_words(entities.get(i).getName());
                RxBus.getDefault().post(action);
            }
        });
    }

    @Override
    public void setupData() {
        presenter.getHotWords();
    }

    @Override
    public void setHotWords(HotWordsBean hot_wrod_bean) {
        if (hot_wrod_bean.getData() != null && hot_wrod_bean.getData().getHw() != null) {
            entities.clear();
            entities.addAll(hot_wrod_bean.getData().getHw());
            adapter.SetDatas(entities);
        }
    }

    @Override
    public void setSearchResults(SearchResultBean search_result) {

    }

    @Override
    public void setRecommandSearch(SearchResultBean recommand) {

    }

    @Override
    public void showLoading() {
        progressDialog.show();
    }

    @Override
    public void hideLoading() {
        progressDialog.hide();
    }

    @Override
    public void showError(String errMsg) {
//        Snackbar.make(content_list, errMsg, Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(getActivity()).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }
}
